package com.example.zhxy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.zhxy.model.Admin;
import com.example.zhxy.model.LoginInfo;
import com.example.zhxy.service.AdminService;
import com.example.zhxy.mapper.AdminMapper;
import com.example.zhxy.util.MD5;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
* @author fzykd
* @description 针对表【tb_admin】的数据库操作Service实现
* @createDate 2023-03-13 21:26:35
*/
@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin>
    implements AdminService{

    //登录校验
    @Override
    public Admin login(LoginInfo loginInfo) {
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name",loginInfo.getUsername());
        //数据库中存储的是密文 MD5加密之后在对比
        queryWrapper.eq("password", MD5.encrypt(loginInfo.getPassword()));
        Admin admin = baseMapper.selectOne(queryWrapper);
        return admin;
    }

    @Override
    public Admin getAdminById(Long userId) {
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",userId);
        return baseMapper.selectOne(queryWrapper);
    }


    //管理员管理 分页查询
    @Override
    public IPage<Admin> getAdminPage(Page<Admin> page, String adminName) {
        LambdaQueryWrapper<Admin> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(!StringUtils.isEmpty(adminName),Admin::getName,adminName);

        queryWrapper.orderByAsc(Admin::getId);

        return baseMapper.selectPage(page,queryWrapper);
    }
}




